package com.example.fund.util;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.fund.R;
import com.example.fund.info.FOFInfo;
import com.example.fund.info.FundInfo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FOFUtil {

    public static List<Map<String, Object>> getFOFInfoList()
    {
        List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
        String sql = "select distinct account from fundTrade where account is not null order by account ";
        SQLiteDatabase db = DBManager.getDB();
        Cursor c = db.rawQuery(sql, null);
        int i = 0;
        while (c.moveToNext())
        {
            String name = c.getString(c.getColumnIndex("account"));
            String number = "FOF";
            if (i<10)
            {
                number = number+"0"+i;
            }
            String pinYin = number;
            String type = "2";

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("isFOF", "1");
            map.put("number", number);
            map.put("pinYin", pinYin);
            map.put("name", name);
            map.put("type", type);
            map.put("img", R.drawable.star4);

            list.add(map);
        }
        c.close();

        return list;
    }

    public static FOFInfo getFOFNetValue(String name,String number)
    {
        FOFInfo fofInfo = new FOFInfo();
        fofInfo.setNumber(number);
        fofInfo.setName(name);

  //      FundInfo info = FundUtil.getFundNetValueFromLocalhost("","1399967");
  //      fofInfo.setActualValues(info.getActualValues());
        List<FundInfo> fundInfos = FundTradeUtil.getFundInfoListByAccount(name);
        fofInfo.setFundInfos(fundInfos);

        //计算基金的累积份额
        List<String> dateList = FundUtil.getSHIndexDateList();
        for (int j=0;j<fundInfos.size();j++)
        {
            FundInfo fundInfo = fundInfos.get(j);
            Map<String,Double> tradeQtyMap = fundInfo.getFundTradeQtyMap();
            boolean isFind = false;
            double tradeQtyTotal = 0;
            for (int i=dateList.size()-1;i>=0;i--) {
                String dateStr = dateList.get(i);
                Double tradeQty = tradeQtyMap.get(dateStr);
                if (!isFind && tradeQty!=null)
                {
                    isFind = true;
                }
                if (isFind)
                {
                    if (tradeQty!=null)
                    {
                        tradeQtyTotal+=tradeQty;
                    }
                    tradeQtyMap.put(dateStr,tradeQtyTotal);
                }
            }
        }



        fofInfo.setActualValues(getActualValues(fofInfo,dateList));



        return fofInfo;
    }

    public static Map<String, String> getActualValues(FOFInfo fofInfo,List<String> dateList)
    {
        Map<String, String> retActValues = new HashMap<String,String>();
        List<FundInfo> fundInfos = fofInfo.getFundInfos();
        if (fofInfo==null || fundInfos==null
            || fundInfos.size()==0
            || dateList==null || dateList.size()==0)
        {
            return retActValues;
        }

        double netValue = 1.0;
        for (int i=dateList.size()-2;i>=0;i--)
        {
            String lastDateStr = dateList.get(i+1);
            String dateStr = dateList.get(i);
            double lastValueTotal = 0;
            double valueTotal = 0;
            for (int j=0;j<fundInfos.size();j++)
            {
                FundInfo info = fundInfos.get(j);
                Map<String,Double> tradeQtyMap = info.getFundTradeQtyMap();
                Map<String, String> actValues = info.getActualValues();
                String lastPrice = actValues.get(lastDateStr);
                String price = actValues.get(dateStr);
                if (StringUtil.isNullOrEmpty(lastPrice) || StringUtil.isNullOrEmpty(price))
                {
                    continue;
                }
                Double lastTradeQty = tradeQtyMap.get(lastDateStr);
                if (lastTradeQty!=null)
                {
                    lastValueTotal+=lastTradeQty*Double.valueOf(lastPrice);
                    valueTotal+=lastTradeQty*Double.valueOf(price);
                }
            }

            if (lastValueTotal>0 && valueTotal>0)
            {
                netValue = netValue*(1.0+(valueTotal-lastValueTotal)/lastValueTotal);
                retActValues.put(dateStr,String.valueOf(netValue));
            }
        }

        return retActValues;
    }

}
